<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User,App\Role,Auth,App\permission;
use App\Http\Requests\AuthRequest;
class AdminController extends Controller
{
    public $authRole;
    public $auth;
    public function __construct()
    {
        $this->middleware('auth');

        if($this->auth = Auth::user()){
            $this->authRole = User::find($this->auth->id)->roles[0];
        }

    }
    public function getIndex()
    {
        $users = User::paginate(3);
        foreach($users as $user)
        {
            $user['role'] = User::find($user->id)->roles;
        }
        $authRole = $this->authRole;
        $perms = Role::find($authRole->id)->perms;
        foreach($perms as $v){
            if($v->name == 'add'){
                unset($v->name);
            }
        }
        if($this->auth->can('add')){
            $add = Permission::where('name','=','add')->first();
        }
        return view('admin.index',compact('users','perms','add','authRole'));
    }
    public function getView($id)
    {
        $authRole = $this->authRole;
        $user = User::find($id);
        $user['role'] = User::find($user->id)->roles[0];
        return view('admin.view',compact('authRole','user'));
    }
    public function getEdit($id)
    {
        $authRole = $this->authRole;
        $user = User::find($id);
        $user['role'] = User::find($user->id)->roles[0];
        $allRoles = Role::all();
        return view('admin.edit',compact('authRole','user','allRoles'));
    }
    public function postEdit(Request $request,$id)
    {
        $user = User::find($id);

        $this->validate($request,
            ['name' =>  'required|min:6|max:255|unique:users,name,'.$id]    //验证时忽略自身name
        );
        User::where('id',$id)->update($request->only('name'));

    //     $article->update($request->except(['_token','tags']));

        if($request->role){
            $user->roles()->sync( $request->role );
        }

    //     dd($request->all());
        return redirect()->back()->withInfo('修改成功');
    }
    public function getDel($id)
    {
        $authRole = $this->authRole;
        return view('admin.del',compact('authRole'));
    }
    public function postDel(Request $request){
        $user = User::findOrFail($request->id);
        $roleIds = \DB::table('role_user')->where('user_id',$request->id)->pluck('role_id');
        $user->roles()->detach($roleIds);
        User::destroy($request->id);
        return '删除成功';
    }
    public function getAdd()
    {
        $authRole = $this->authRole;
        $allRoles = Role::all();
        return view('admin.add',compact('authRole','allRoles'));
    }
    public function postAdd(AuthRequest $request)
    {
        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => bcrypt($request->password)
        ]);

        $user->roles()->attach($request->role);  //注册后成为普通用户

        return redirect()->back()->with('info','添加用户成功');
    }



}
